package com.xn.app.muying.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.xn.app.muying.R;
import com.xn.app.muying.sqlite.DBManager;
import com.xn.app.muying.util.BaseActivity;

/**
 * 体温监测-通知类型设置
 * Created by gaoge on 2017/2/10.
 */
public class TemperatureMusicActivity extends BaseActivity implements View.OnClickListener {
    //标题
    private TextView title_name = null;
    //返回
    private ImageView img_back = null;
    //震动开关
    private ToggleButton vibration = null;
    //音乐开关
    private ToggleButton music = null;
    //数据库
    private DBManager db = null;
    //保存提醒状态
    private int type = 0; //0 铃声  1 震动  2 铃声+震动  3 无提醒

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加载布局
        setContentView(R.layout.temperature_music_activity);
        //初始化
        init();
    }

    //初始化组件
    private void init() {
        db = new DBManager(this);
        title_name = (TextView) findViewById(R.id.title_name);
        img_back = (ImageView) findViewById(R.id.img_back);
        vibration = (ToggleButton) findViewById(R.id.vibration);
        music = (ToggleButton) findViewById(R.id.music);
        //设置标题
        title_name.setText("消息类型设置");
        img_back.setOnClickListener(this);
        vibration.setOnClickListener(this);
        music.setOnClickListener(this);
        //获取用户设置
        type = Integer.parseInt(db.selectTemperature("RemindWay"));
        //设置按钮
        setButton(type);
    }

    /**
     * 设置按钮选择
     *
     * @param type 属性
     */
    public void setButton(int type) {
        switch (type) {
            //铃声
            case 0:
                vibration.setChecked(false);
                music.setChecked(true);
                break;
            //震动
            case 1:
                vibration.setChecked(true);
                music.setChecked(false);
                break;
            //铃声+震动
            case 2:
                vibration.setChecked(true);
                music.setChecked(true);
                break;
            //无提醒
            case 3:
                vibration.setChecked(false);
                music.setChecked(false);
                break;
        }
    }

    //单击事件
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //返回键
            case R.id.img_back:
                finish();
                break;
            //震动开关
            case R.id.vibration:
                if (!vibration.isChecked()) {
                    vibration.setChecked(false);
                    if (music.isChecked()) {
                        db.updateTemperatureSetSingle("RemindWay", 0);
                    } else {
                        db.updateTemperatureSetSingle("RemindWay", 3);
                    }
                } else {
                    vibration.setChecked(true);
                    if (music.isChecked()) {
                        db.updateTemperatureSetSingle("RemindWay", 2);
                    } else {
                        db.updateTemperatureSetSingle("RemindWay", 1);
                    }
                }
                break;
            //音乐开关
            case R.id.music:
                if (!music.isChecked()) {
                    music.setChecked(false);
                    if (vibration.isChecked()) {
                        db.updateTemperatureSetSingle("RemindWay", 1);
                    } else {
                        db.updateTemperatureSetSingle("RemindWay", 3);
                    }
                } else {
                    music.setChecked(true);
                    if (vibration.isChecked()) {
                        db.updateTemperatureSetSingle("RemindWay", 2);
                    } else {
                        db.updateTemperatureSetSingle("RemindWay", 0);
                    }
                }
                break;
        }
    }
}
